<template>
  <div class="login-container">
    <!-- 登录 -->
    <el-form
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      class="login-form"
    >
      <div class="login-title">
        <h3 class="title" align="center" v-if="changeLogin == true">登&ensp;录</h3>
      </div>
      <div class="login-title" v-if="changeLogin == false">
        <h3 class="title" align="center">注&ensp;册</h3>
      </div>
      <el-form-item prop="username">
        <el-input
          placeholder="username"
          v-model="loginForm.username"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="password"
          v-model="loginForm.password"
        ></el-input>
      </el-form-item>
      <el-form-item prop="repassword" v-if="changeLogin == false">
        <el-input
          placeholder="repassword"
          v-model="loginForm.repassword"
        ></el-input>
      </el-form-item>
      <div v-if="changeLogin == true">
        <el-button
          style="width: 100%; margin-bottom: 30px; margin-top: 10px"
          type="primary"
          @click="login"
          >登录</el-button
        >
        <br />
        <el-button
          style="width: 100%; font-size: 17px"
          @click="changeLogin = false"
          >切换为注册</el-button
        >
      </div>
      <div v-if="changeLogin == false">
        <el-button
          style="width: 100%; margin-bottom: 30px; margin-top: 10px"
          type="primary"
          @click="regUser"
          >注册</el-button
        >
        <br />
        <el-button
          style="width: 100%; font-size: 17px"
          @click="changeLogin = true"
          >切换为登录</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { regUser } from "../../api/login";
export default {
  data() {
    return {
      // 登录表单
      loginForm: {
        username: "",
        password: "",
        repassword: "",
      },
      // 登录验证
      loginRules: {
        username: [
          { required: true, message: "username is required", trigger: "blur" },
        ],
        password: [
          { required: true, message: "password is required", trigger: "blur" },
        ],
        repassword: [
          {
            required: true,
            message: "repassword is required",
            trigger: "blur",
          },
        ],
      },
      changeLogin: true,
    };
  },
  mounted() {},
  methods: {
    login() {
      this.$refs["loginForm"].validate(async (valid) => {
        if (valid) {
          let res = await this.$store.dispatch("user/login", this.loginForm);
          this.loginForm = {
              username: '',
              password: '',
          }
          this.$router.push('/layout')
        } else {
          this.$message.error({
            message: "用户名或密码不能为空",
            offset: 60,
          });
        }
      });
    },
    regUser() {
      this.$refs["loginForm"].validate(async (valid) => {
        if (valid) {
          let { username, password, repassword } = this.loginForm
          let res = await regUser(username, password, repassword);
          this.loginForm = {
              username: '',
              password: '',
              repassword: '',
          }
          this.changeLogin = true
        } else {
          this.$message.error({
            message: "用户名或密码不能为空",
            offset: 60,
          });
        }
      });
    },
  },
};
</script>

<style scoped lang='scss'>
.login-container {
  width: 100%;
  height: 100%;
  background-color: #2f3a4b;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    width: 520px;
    padding: 50px;

    .login-title {
      .title {
        font-size: 26px;
        color: #fff;
      }
    }
    .el-form-item {
      line-height: 50px;
      ::v-deep input {
        background-color: #293543;
        color: #c0c4c3;
        height: 50px;
        border: 1px solid #394553;
      }
    }
  }
}
</style>
